<?php
/**
 * 基础接口
 * @author shitou
 *
 */
class QiniuUpload {
	/**
     * 生产token
     * @access public
     * @return array
     */
	public function createToken(){
		vendor("Qiniu/rs");

		Qiniu_SetKeys(C('QINIU_ACCESS_KEY'), C('QINIU_SECRET_KEY'));
		$putPolicy = new Qiniu_RS_PutPolicy(C('QINIU_BUCKET'));
		$upToken = $putPolicy->Token(null);
		
		return $upToken;
    }
	
    /**
     * 创建文件夹
     * @access public
     * @return array
     */
	public function createFolder($dir){
		return 1;
    }
	
	/**
     * 删除文件夹
     * @access public
     * @return array
     */
	public function deleteFolder($folderkey){
		return 1;
    }
	
	/**
     * 文件路径
     * @access public
     * @return string
     */
	public function uploadFile($arr){
		$extarr = explode('.',$arr['title']);
		$ext = $extarr[count($extarr)-1];
		if(in_array($ext,C('FL_imageExts'))){
			$Upload_file = M("UploadFile");
			$Upload_file->filetitle = $arr['title'];
			$Upload_file->folder_id = $arr['folder_id'];
			$Upload_file->filekey = $arr['filekey'];
			$Upload_file->filetype = $ext;
			$Upload_file->addid = $arr['addid'];
			$Upload_file->addtime = time();
			$Upload_file->add();
			
			$result['status'] = 1;
			$result['info'] = '上传成功';
		}else{
			$result['status'] = 0;
			$result['info'] = '上传失败，文件不在允许上传的范畴';
		}
		
		return $result;
	}
	
	/**
     * 删除文件
     * @access public
     * @return array
     */
	public function deleteFile($filekey){
		vendor("Qiniu/http");
		vendor("Qiniu/rs");
		
		$client = new Qiniu_MacHttpClient(null);
		$data = Qiniu_RS_Delete($client,C('QINIU_BUCKET'),$filekey);
    }
	
	/**
     * 文件路径
     * @access public
     * @return string
     */
	public function fileUrl($filekey,$thumb='',$integral=true){
		$fileurl = '';
		
		if($integral){
			$filepath = C('QINIU_DOMAIN').$filekey;
		}else{
			$filepath = $filekey;
		}
		if(empty($thumb)){
			$fileurl = $filepath;			
		}else{
			$fileurl = $filepath.$thumb;
		}
		
		return $fileurl;
	}
}
?>